<script setup lang="ts">
import { onMounted } from 'vue';
import { state } from './state';
import dayjs from 'dayjs';
import { navigateTo, showLoading, reLaunch } from '@tarojs/taro';
import { APP_SCHEDULE_RECORD_LIST_QUERY } from '/@/api/modules/base/schedule-app';
import { LOGIN_APP } from '/@/api/modules/base/app-sys-user';

/**
 * @description 排班记录列表查询
 * @param {Number} page -当前页
 * @param {Number} pageSize -每页条数
 */
function queryList(page: number = 1, pageSize: number = 15) {
  if (!page && state.finished) return;
  state.loading = true;

  APP_SCHEDULE_RECORD_LIST_QUERY({
    comQuery: { page, pageSize },
    userId: state.appUserId,
    type: 1,
  })
    .then((res) => {
      state.loading = false;

      if (
        state.activePage >=
        (res.comQuery?.pageCount ||
          (res.comQuery?.totalCount as number) / (res.comQuery?.pageSize as number) ||
          1)
      )
        state.finished = true;
      if (!res.content || !res.content.length) return;

      res.content.forEach((item) => state.list.push(item));

      state.activePage += 1;
    })
    .catch(() => {
      state.loading = false;
    });
}

/**
 * @function 加载
 */
function onLoading() {
  if (state.finished) return;

  queryList(state.activePage);
}

onMounted(() => {
  showLoading();
  LOGIN_APP().then((res) => {
    state.appUserId = res.sysUserDto.id || '';
    refreshList();
  });
});

/**
 * 刷新列表
 */
function refreshList() {
  state.activePage = 1;
  state.loading = true;
  state.finished = false;
  state.list = [];
  onLoading();
}
</script>

<template>
  <view class="swap-start-list app-page--1">
    <app-navbar title="交接班" />
    <nut-tabs
      model-value="record"
      @click="
        ({ paneKey }) => {
          if (paneKey === 'start') {
            reLaunch({ url: '/pages/swap/start/index' });
          }
        }
      "
    >
      <nut-tab-pane title="发起交接" pane-key="start" />
      <nut-tab-pane title="交接记录" pane-key="record" />
    </nut-tabs>
    <scroll-view
      class="app-page__wrapper"
      :scroll-y="true"
      :lower-threshold="100"
      @scrolltolower="onLoading()"
    >
      <view class="swap-start-list__wrapper">
        <view
          class="swap-start-item"
          v-for="item in state.list"
          :key="item.id"
          @click="
            navigateTo({
              url: `/pages/swap/detail/index?id=${item.id}`,
            })
          "
        >
          <view class="swap-start-item__header">
            <view class="swap-start-item__header--left">
              <text v-if="item.recordType === 1">我的交班</text>
              <text v-else>我的接班</text>
            </view>
            <view
              class="swap-start-item__header--right"
              :style="{
                color: item.status === 2 ? '#1ABE96' : '#ee4e36',
                borderColor: item.status === 2 ? '#1ABE96' : '#ee4e36',
              }"
            >
              <text>{{ item.status === 2 ? '已完成' : '未签收' }}</text>
            </view>
          </view>
          <view>
            <text>完成时间:&nbsp;&nbsp;</text>
            <text>{{
              item.status === 2 ? dayjs(item.checkTime).format('YYYY-MM-DD HH:mm') : '未完成'
            }}</text>
          </view>
          <view>
            <text>交接班时:&nbsp;&nbsp;</text>
            <text>{{ item.scheduleName || '_' }}</text>
          </view>
          <view v-if="item.recordType === 1">
            <text>接班人:&nbsp;&nbsp;</text>
            <text>{{ item.overPerson || '_' }}</text>
          </view>
          <view v-else>
            <text>交班人:&nbsp;&nbsp;</text>
            <text>{{ item.shiftPerson || '_' }}</text>
          </view>
        </view>
      </view>
      <view class="app-page__list-tips">
        <text v-if="!state.list.length">暂无数据！</text>
        <text v-else-if="state.list.length && state.loading">加载中...</text>
        <text v-else-if="state.list.length && state.finished">加载完成!</text>
      </view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
@import './index.scss';
</style>
